package com.ruoyi.project.tickets.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.tickets.domain.OutStockDetails;
import com.ruoyi.project.tickets.domain.ShopUser;
import com.ruoyi.project.tickets.domain.Stock;
import com.ruoyi.project.tickets.domain.dto.OutStockDto;
import com.ruoyi.project.tickets.domain.query.OutStockQuery;
import com.ruoyi.project.tickets.domain.vo.OutStockVo;
import com.ruoyi.project.tickets.mapper.StockMapper;
import com.ruoyi.project.tickets.service.IOutStockDetailsService;
import com.ruoyi.project.tickets.service.IShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.tickets.mapper.OutStockMapper;
import com.ruoyi.project.tickets.domain.OutStock;
import com.ruoyi.project.tickets.service.IOutStockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库记录Service业务层处理
 *
 * @author 孙山伟
 * @date 2023-07-01
 */
@Service
public class OutStockServiceImpl implements IOutStockService {
    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IShopUserService shopUserService;
    @Autowired
    private IOutStockDetailsService outStockDetailsService;

    /**
     * 查询出库记录
     *
     * @param id 出库记录ID
     * @return 出库记录
     */
    @Override
    public OutStock selectOutStockById(Long id) {
        return outStockMapper.selectOutStockById(id);
    }

    /**
     * 查询出库记录列表
     *
     * @param outStock 出库记录
     * @return 出库记录
     */
    @Override
    public List<OutStockVo> selectOutStockList(OutStockQuery outStock) {
        return outStockMapper.selectOutStockList(outStock);
    }

    /**
     * 新增出库记录
     *
     * @param outStockList 出库记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOutStock(List<OutStockDto> outStockList)  {
        try {
            LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
            ShopUser shopUser = new ShopUser();
            shopUser.setUserId(user.getUser().getUserId());
            List<ShopUser> shopUserList = shopUserService.selectShopUserList(shopUser);
            if (ObjectUtil.isNull(shopUserList)) {
                shopUser.setShopId(-1L);
            } else {
                if (shopUserList.size() == 0) {
                    shopUser.setShopId(-1L);
                } else {
                    shopUser.setShopId(shopUserList.get(0).getShopId());
                }
            }
            Long sum = outStockList.stream().mapToLong(OutStockDto::getNumber).sum();
            OutStock outStock = new OutStock();
            outStock.setOutNumber(sum);
            outStock.setOutTime(new Date());
            outStock.setShopId(shopUser.getShopId());
            outStock.setUserId(user.getUser().getUserId());
            outStockMapper.insertOutStock(outStock);
            for (OutStockDto dto : outStockList) {
                Stock stock = stockMapper.selectStockById(dto.getStockId());
                if (stock.getStockNum() == 0) {
                    throw new Exception("库存不足，不允许出库");
                }
                OutStockDetails details = new OutStockDetails();
                details.setOutNumber(dto.getNumber());
                details.setOutStockId(outStock.getId());
                details.setRuleId(stock.getRuleId());
                details.settUserId(user.getUser().getUserId());
                details.settShopId(shopUser.getShopId());
                details.setCreateTime(new Date());
                outStockDetailsService.insertOutStockDetails(details);
                stock.setStockNum(stock.getStockNum()-dto.getNumber());
                stockMapper.updateStock(stock);
            }

            return 1;
        }catch (Exception e){
            e.getMessage();
            return 0;
        }
    }

    /**
     * 修改出库记录
     *
     * @param outStock 出库记录
     * @return 结果
     */
    @Override
    public int updateOutStock(OutStock outStock) {
        outStock.setUpdateTime(DateUtils.getNowDate());
        return outStockMapper.updateOutStock(outStock);
    }

    /**
     * 批量删除出库记录
     *
     * @param ids 需要删除的出库记录ID
     * @return 结果
     */
    @Override
    public int deleteOutStockByIds(Long[] ids) {
        return outStockMapper.deleteOutStockByIds(ids);
    }

    /**
     * 删除出库记录信息
     *
     * @param id 出库记录ID
     * @return 结果
     */
    @Override
    public int deleteOutStockById(Long id) {
        return outStockMapper.deleteOutStockById(id);
    }


}
